<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/4/16
 * Time: 11:49
 */

namespace common\logics\taskorderlog;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;

class TaskOrderLogLogic extends BaseObject implements ITaskOrderLog
{
    /** @var \common\sources\read\taskorderlog\BuyTaskOrderLogAccess $RBuyTaskOrderLogAccess **/
    private $RBuyTaskOrderLogAccess;
    /** @var \common\sources\write\taskorderlog\BuyTaskOrderLogAccess $WBuyTaskOrderLogAccess **/
    private $WBuyTaskOrderLogAccess;
    /** @var \common\sources\read\taskorder\TaskOrderAccess  $RTaskOrderAccess  ***/
    private $RTaskOrderAccess;
    /** @var \common\sources\write\taskorder\TaskOrderAccess  $WTaskOrderAccess  **/
    private $WTaskOrderAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess ***/
    private $RBuyUserAccess;
    /** @var \common\sources\read\buyuser\BuyUserAcountAccess  $RBuyUserAcountAccess **/
    private $RBuyUserAcountAccess;
    /** @var \common\sources\write\buyuser\BuyUserAcountAccess  $WBuyUserAcountAccess  ***/
    private $WBuyUserAcountAccess;
    /** @var \common\sources\read\businessaccount\BusinessAccountAccess $RBusinessAccountAccess  **/
    private $RBusinessAccountAccess;
    /** @var \common\sources\write\buydepositrecord\BuyDepositRecordAccess  $WBuyDepositRecordAccess  **/
    private $WBuyDepositRecordAccess;
    /** @var \common\sources\write\buypepperrecord\BuyPepperRecordAccess  $WBuyPepperRecordAccess ***/
    private $WBuyPepperRecordAccess;
    /** @var \common\sources\write\businessdepositrecord\BusinessDepositRecordAccess  $WBusinessDepositRecordAccess **/
    private $WBusinessDepositRecordAccess;
    /** @var \common\sources\write\businesspepperrecord\BusinessPepperRecordAccess  $WBusinessPepperRecordAccess ****/
    private $WBusinessPepperRecordAccess;
    /** @var \common\sources\write\businessacount\BusinessAcountAccess $WBusinessAcountAccess  **/
    private $WBusinessAcountAccess;
    /** @var \common\sources\write\taskpepperrecord\TaskPepperRecordAccess  $WTaskPepperRecordAccess  **/
    private $WTaskPepperRecordAccess;
    /** @var \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess **/
    private $RTaskCommodAccess;
    /** @var \common\sources\read\businesspepperrecord\BusinessPepperRecordAccess  $RBusinessPepperRecordAccess **/
    private $RBusinessPepperRecordAccess;
    /** @var \common\sources\read\businessdepositrecord\BusinessDepositRecordAccess $RBusinessDepositRecordAccess **/
    private $RBusinessDepositRecordAccess;
    /** @var \common\sources\write\taskcommod\TaskCommodAccess $WTaskCommodAccess **/
    private $WTaskCommodAccess;

    public function init()
    {
        $this->RBuyTaskOrderLogAccess = Yii::$container->get("RBuyTaskOrderLogAccess");
        $this->WBuyTaskOrderLogAccess = Yii::$container->get("WBuyTaskOrderLogAccess");
        $this->RTaskOrderAccess = Yii::$container->get("RTaskOrderAccess");
        $this->WTaskOrderAccess = Yii::$container->get("WTaskOrderAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        $this->RBuyUserAcountAccess = Yii::$container->get("RBuyUserAcountAccess");
        $this->WBuyUserAcountAccess = Yii::$container->get("WBuyUserAcountAccess");
        $this->RBusinessAccountAccess = Yii::$container->get("RBusinessAccountAccess");
        $this->WBuyDepositRecordAccess = Yii::$container->get("WBuyDepositRecordAccess");
        $this->WBuyPepperRecordAccess = Yii::$container->get("WBuyPepperRecordAccess");
        $this->WBusinessDepositRecordAccess = Yii::$container->get("WBusinessDepositRecordAccess");
        $this->WBusinessPepperRecordAccess = Yii::$container->get("WBusinessPepperRecordAccess");
        $this->WBusinessAcountAccess = Yii::$container->get("WBusinessAcountAccess");
        $this->WTaskPepperRecordAccess = Yii::$container->get("WTaskPepperRecordAccess");
        $this->RTaskCommodAccess = Yii::$container->get("RTaskCommodAccess");
        $this->RBusinessPepperRecordAccess = Yii::$container->get("RBusinessPepperRecordAccess");
        $this->RBusinessDepositRecordAccess = Yii::$container->get("RBusinessDepositRecordAccess");
        $this->WTaskCommodAccess = Yii::$container->get("WTaskCommodAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    //增加日志
    public function addTaskOrderLog($data)
    {
        if(empty($data['operator']))
        {
            return Tool::returnJson([],401,"操作人不能为空");
        }
        if(empty($data['type']))
        {
            return Tool::returnJson([],402,"操作类型不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],403,"子任务id不能为空");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],405,"子订单不存在");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($task_order['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],406,"买手账号不存在");
        }
        //1.操作商家发货
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $task_commod = $this->RTaskCommodAccess->getTaskDetail($task_order['task_commod_id']);
            if($data['type'] == 1)
            {
                if($task_order['order_status'] != 4)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],406,"不能对子订单操作发货");
                }
                $arr = [
                    "task_order_id" => $data['task_order_id'],
                    "order_status" => 5
                ];
                $res = $this->WTaskOrderAccess->deliverGoods($arr);
                if(!$res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],407,"发货失败");
                }
                //增加发货日志
                $task_order_arr = [
                    "buy_id" => 0,
                    "task_status" => 5,
                    "status_remark" => "商家已发货，待买手确认收货",
                    "remark" => '已发货待收货',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],408,"添加后台商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 2)//操作买手收货
            {
                if($task_order['order_status'] != 5)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],409,"不能对子订单进行收货操作");
                }
                //收货操作
                $arr = [
                    "task_order_id" => $data['task_order_id'],
                    "order_status" => 6
                ];
                $res = $this->WTaskOrderAccess->receivingGood($arr);
                if(!$res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],410,"收货操作失败");
                }
                //增加发货日志
                $task_order_arr = [
                    "buy_id" => 0,
                    "task_status" => 6,
                    "status_remark" => "买手已收货,待商家返款",
                    "remark" => '已收货待返款',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],411,"添加后台商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 3)//撤销买手收货
            {
                if(!($task_order['order_status'] == 6 ||$task_order['order_status'] == 9) )
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],412,"不能对子订单进行撤销买手收货操作");
                }
                //撤销买手收货操作
                $arr = [
                    "task_order_id" => $data['task_order_id'],
                    "order_status" => 5
                ];
                $res = $this->WTaskOrderAccess->revokeReceive($arr);
                if(!$res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],413,"撤销买手收货失败");
                }
                //增加发货日志
                $task_order_arr = [
                    "buy_id" => 0,
                    "task_status" => 5,
                    "status_remark" => "商家已发货,待买手收货好评",
                    "remark" => '已发货待收货',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],415,"添加后台商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 4)//.操作商家返款
            {
                if(!($task_order['order_status'] == 6 ||$task_order['order_status'] == 9))
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],416,"不能对子订单进行返款操作");
                }
                //返款操作
	            $arr = [
		            "task_order_id"  => $data['task_order_id'],
		            "order_status"   => 7,
		            "actual_price"   => $task_order['advance_amount'],
		            "refund_amount"  => $task_order['advance_amount'],
		            "advance_amount" => $task_order['advance_amount'],
		            "actual_refunds" => $task_order['advance_amount'],
	            ];
                $result = $this->WTaskOrderAccess->refunds($arr);
                if(!$result)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],417,"操作商家返款失败");
                }
                //操作商家返款日志
                $task_order_arr = [
                    "buy_id" => 0,
                    "task_status" => 7,
                    "status_remark" => "商家已返款,待买手确认返款",
                    "remark" => '已返款待确认',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],418,"添加后台商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 5)//操作买手确认返款
            {
                if($task_order['order_status'] != 7)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],419,"不能对子订单进行确认返款操作");
                }
                //买手确认返款
                $arr = [
                    "task_order_id" => $data['task_order_id'],
                    "order_status" => 8
                ];
                $result = $this->WTaskOrderAccess->confirmRefunds($arr);
                if(!$result)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],420,"操作买手确认返款失败");
                }
                //根据返款类型进行返款
                $buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($task_order['buy_id']);
                if($task_order['return_type'] == 1)
                {
                    //增加买手本金记录
                    $deposit_arr = [
                        "buy_id" => $task_order['buy_id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_id" => $task_order['task_id'],
                        "task_order_num" => $task_order['task_order_num'],
                        "task_order_id" => $task_order['id'],
                        "expend" => 0,
                        "income" => number_format($task_order['refund_amount'],2,".",""),
                        "balance" => number_format($task_order['refund_amount'] + $buy_account['remain_funds'],2,".",""),
                        "freeze_change"=> 0,
                        "freeze_balance"=> number_format($buy_account['freezing_funds']),
                        "remark" => "完成任务获得本金",
                        "operatorId" => $data['operator'],
                        "end_user" => $data['operator'],
                        'type'       =>1
                    ];
                    $deposit_res = $this->WBuyDepositRecordAccess->addBuyDepositRecord($deposit_arr);
                    if(!$deposit_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],421,"增加买手本金记录失败");
                    }
                    //增加买手佣金记录
                    $pepper_arr = [
                        "buy_id" => $task_order['buy_id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_id" => $task_order['task_id'],
                        "task_order_num" => $task_order['task_order_num'],
                        'task_order_id' => $task_order['id'],
                        "expend" => 0,
                        "income" => number_format($task_order['commission'],2,".",""),
                        "balance" => number_format($task_order['commission'] + $buy_account['remail_golds'],2,".",""),
                        "freeze_change"=> 0,
                        "freeze_balance"=> number_format($buy_account['freezing_golds'],2,".",""),
                        "remark" => "完成任务获得花椒",
                        "operatorId" => $data['operator'],
                        "end_user" => $data['operator'],
                        "type" => 1,
                    ];
                    $pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
                    if(!$pepper_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],422,"增加佣金记录异常");
                    }
                    //返还商家押金 5%
                    $deposit = number_format($task_order['price'] * $task_order['num'] * 0.05,2,".","");
                    if($deposit >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],423,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            "type" => 3,
                            "income" => $deposit,
                            "balance" =>number_format( $business_account['ava_deposit'] + $deposit,2,".",""),
                            "freeze_change"=> -$deposit,
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $deposit,2,".",""),
                            "remark" => "买手完成任务返还任务保证金",
                            "operatorId" =>$data['operator'],
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],425,"买手完成任务返还任务保证金");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $deposit,2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $deposit,2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'] - $task_order['commission'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],426,"修改商家账号信息异常");
                        }
                    }

                    //运费返还
                    if($task_commod['freight_deposit'] >0)
                    {
                        $freight_deposit = number_format($task_commod['freight_deposit'],2,".","");
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => $freight_deposit,
                            "balance" =>number_format( $business_account['ava_deposit'] + $freight_deposit,2,".",""),
                            "freeze_change"=> -$freight_deposit,
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $freight_deposit,2,".",""),
                            "remark" => "买手完成任务返还商家运费押金",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],416,"买手完成任务返还任务运费押金异常");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $freight_deposit,2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $freight_deposit,2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],417,"修改商家账号信息异常");
                        }
                    }
                    //修改 相等的情况
                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","") == 0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        //增加本金记录
                        $bus_depsite_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>number_format( $business_account['ava_deposit'],2,".",""),
                            "freeze_change"=> -$task_order['refund_amount'],
                            "freeze_balance" =>  number_format($business_account['freezing_deposit'] - $task_order['refund_amount'],2,".",""),
                            "remark" => "买手完成任务扣减商家押金给买手",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_deposite_ress = $this->WBusinessDepositRecordAccess->addDepositRecord($bus_depsite_arr);
                        if(!$bus_deposite_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4115,"买手完成任务扣减商家押金给买手失败");
                        }
                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>number_format( $business_account['ava_pepper'],2,".",""),
                            "freeze_change"=> - number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - $task_order['refund_amount'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['refund_amount'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"修改商家账号信息异常");
                        }
                    }
                    //修改商家扣减押金金额不一致情况  少扣押金的情况
                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","")>0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $ava_deposit = number_format( $business_account['ava_deposit'] -($task_order['refund_amount']-$task_order['price']* $task_order['num']),2,".","");
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".",""),
                            "income" => 0,
                            'type' => 2,
                            "balance" =>$ava_deposit,
                            "freeze_change"=> -number_format($task_order['price']* $task_order['num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_order['price']* $task_order['num'],2,".",""),
                            "remark" => "买手完成任务扣减押金不足的部分金额".($task_order['refund_amount']-$task_order['price']* $task_order['num']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],435,"增加商家任务返款押金不足部分金额扣减失败");
                        }

                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>-$business_account['ava_pepper'],
                            "freeze_change"=> -number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }

                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - ($task_order['refund_amount']-$task_order['price']* $task_order['num']) - $task_order['price']* $task_order['num'],2,".",""),
                            "ava_deposit" => $ava_deposit,
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['price']* $task_order['num'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'] - ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],436,"修改商家账号信息异常");
                        }
                    }
                    //修改商家扣减押金金额不一致情况  多扣押金的情况
                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","")< 0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $ava_deposit = number_format( $business_account['ava_deposit'] + ($task_order['price']* $task_order['num']- $task_order['refund_amount']));
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => number_format($task_order['price']* $task_order['num']- $task_order['refund_amount'],2,".",""),
                            "balance" => $ava_deposit,
                            "freeze_change"=> -number_format($task_order['price']* $task_order['num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - ($task_order['price']* $task_order['num']),2,".",""),
                            "remark" => "买手完成任务扣减押金多余的部分返还金额".($task_order['price']* $task_order['num']- $task_order['refund_amount']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposite_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposite_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],437,"增加商家任务返款押金多余部分金额返还失败");
                        }
                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>-$business_account['ava_pepper'],
                            "freeze_change"=> -number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }

                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - $task_order['refund_amount'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + ($task_order['price']* $task_order['num'] - $task_order['refund_amount']),2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - ($task_order['price']* $task_order['num']),2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'] - ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"修改商家账号信息异常");
                        }
                    }
                    $buy_res = false;
                    //修改买手账号信息
                    if(empty($buy_account))
                    {
                        //增加买手账号记录
                        $buy_arr = [
                            "buy_id" => $task_order['buy_id'],
                            "remain_funds" => number_format($task_order['refund_amount'],2,".",""),
                            "remail_golds" => number_format($task_order['commission'],2,".",""),
                            "freezing_funds" => 0,
                            "freezing_golds" => 0,
                            "income_golds" => 0
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->addUserAccount($buy_arr);
                    }else{
                        //修改买手账号记录
                        $buy_arr = [
                            "buy_id" => $task_order['buy_id'],
                            "remain_funds" => number_format($buy_account['remain_funds'] + $task_order['refund_amount'],2,".",""),
                            "remail_golds" => number_format($buy_account['remail_golds'] + $task_order['commission'],2,".",""),
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->modifyUserAccount($buy_arr);
                    }
                    if(!$buy_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],427,"修改买手账号信息异常");
                    }
                }else if($task_order['return_type'] == 2){
                    //增加佣金记录
                    $pepper_arr = [
                        "buy_id" => $task_order['buy_id'],
                        "task_order_num" => $task_order['task_order_num'],
                        "task_order_id" => $task_order['id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_id" => $task_order['task_id'],
                        "expend" => 0,
                        "income" => number_format($task_order['commission'],2,".",""),
                        "balance" => number_format($task_order['commission'] + $buy_account['remail_golds'],2,".",""),
                        "freeze_change" => 0,
                        "freeze_balance" => number_format($buy_account['freezing_golds'],2,".",""),
                        "remark" => "完成任务获得花椒",
                        "type" => 1,
                    ];
                    $pepper_res =$this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
                    if(!$pepper_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],428,"商家返款增加买手佣金记录异常");
                    }
                    //修改买手账号信息
                    $buy_res = false;
                    if(empty($buy_account))
                    {
                        //增加买手账号记录
                        $buy_arr = [
                            "buy_id" => $task_order['buy_id'],
                            "remain_funds" => 0,
                            "remail_golds" => number_format($task_order['commission'],2,".",""),
                            "freezing_funds" => 0,
                            "freezing_golds" => 0,
                            "income_golds" => number_format($task_order['commission'],2,".",""),
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->addUserAccount($buy_arr);
                    }else
                    {
                        $buy_arr = [
                            "buy_id" => $task_order['buy_id'],
                            "remail_golds" =>number_format( $buy_account['remail_golds'] + $task_order['commission'],2,".",""),
                            "income_golds" => number_format($buy_account['income_golds'] + $task_order['commission'],2,".",""),
                        ];
                        $buy_res  = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($buy_arr);
                    }
                    if(!$buy_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],429,"修改买手账号信息异常");
                    }
                    //返还商家本金和 押金
                    //返还商家押金 5%
                    $deposit = number_format($task_order['price'] * $task_order['num'] * 0.05,2,".","");
                    if($deposit >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],430,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => -$deposit,
                            "balance" =>number_format( $business_account['ava_deposit'] + $deposit,2,".",""),
                            "freeze_change" => 0,
                            "freeze_balance" => number_format($business_account['freezing_deposit']  - $deposit,2,".",""),
                            "remark" => "买手完成任务返还商家任务保证金",
                            'operatorId' => $data['operator']
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],431,"买手完成任务返还商家任务押金异常");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] ,2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $deposit,2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit']  - $deposit,2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'] - $task_order['commission'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],433,"修改商家账号信息异常");
                        }
                    }
                    if($task_commod['freight_deposit'] >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],418,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => $task_commod['freight_deposit'],
                            "balance" =>number_format( $business_account['ava_deposit'] + $task_commod['freight_deposit'],2,".",""),
                            "freeze_change" => -$task_commod['freight_deposit'],
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_commod['freight_deposit'],2,".",""),
                            "remark" => "买手完成任务退还商家运费押金".($task_commod['freight_deposit']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"增加商家任务返款押金不足部分金额扣减失败");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $task_commod['freight_deposit'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_commod['freight_deposit'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],439,"修改商家账号信息退还运费押金异常");
                        }
                    }
                    if($task_order['price'] * $task_order['num'] >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],418,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            "income" => number_format($task_order['price'] * $task_order['num'],2,".",""),
                            'type' => 2,
                            "balance" => number_format($business_account['ava_deposit'] + $task_order['price'] * $task_order['num'],2,".",""),
                            "freeze_change" => -(number_format($task_order['price'] * $task_order['num'],2,".","")),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_order['price'] * $task_order['num'],2,".",""),
                            "remark" => "买手完成任务返还商家押金额-商家返款".number_format($task_order['price'] * $task_order['num'],2,".",""),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusinessDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],435,"增加商家任务返款押金不足部分金额扣减失败");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] ,2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $task_order['price'] * $task_order['num'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['price'] * $task_order['num'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAcountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],421,"修改商家账号信息异常");
                        }
                    }
                }

                //增加买手完成任务获取花椒记录 任务花椒记录
                $task_pepper_array = [
                    "buy_id" => $task_order['buy_id'],
                    "tb_name" => $task_order['tbname'],
                    "task_order_id" => $task_order['id'],
                    "task_order_num" => $task_order['task_order_num'],
                    "pepper_income" => number_format($task_order['commission'],2,".",""),
                    "principal" => number_format($task_order['refund_amount'],2,".",""),
                    "remark" => "完成任务获得花椒"
                ];
                $task_peper_res = $this->WTaskPepperRecordAccess->addTaskPepperRecord($task_pepper_array);
                if(!$task_peper_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],434,"增加买手完成任务获取花椒记录异常");
                }
                //查询主订单下的子订单是否已经接完
                $task_orders_success_count = $this->RTaskOrderAccess->getTaskOrderCompleteCount($task_order['task_commod_id']);
                if($task_orders_success_count == $task_commod['task_num'])//判断所有的子任务是否已经完成
                {
                    $this->WTaskCommodAccess->taskCommodComplete(['id' => $task_order['task_commod_id'],"task_status" => 6]);
                }

                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $task_order['buy_id'],
                    "task_status" => 8,
                    "status_remark" => "已完成",
                    "remark" => '买手确认返款',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],435,"添加买手操作日志失败");
                }
                //增加邀请机制
                if($buyer['pid'] >0)
                {
                    $parentBuyUser = $this->RBuyUserAccess->getBuyUserInfo($buyer['pid']);
                    if($parentBuyUser['role'] ==1)//邀请人是买手
                    {
                        //邀请人返利
                        $parent_buyer_account = $this->RBuyUserAcountAccess->getBuyUserAccount($buyer['pid']);
                        //已经完成任务单数
                        $task_order_count = $this->RTaskOrderAccess->getBuyTaskOrderCount($buyer['id'],8);
                        if($task_order_count ==1)
                        {
                            $inviter_arr = [
                                "buy_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 2,
                                "balance" => number_format($parent_buyer_account['remail_golds'] +2,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $parent_buyer_account['freezing_golds'],
                                "remark" => "邀请人-买手--买手首次完成首单任务获取2花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $buy_pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($inviter_arr);
                            if(!$buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人买手增加返利记录失败");
                            }
                            if(empty($parent_buyer_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" => number_format(2,2,".",""),
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => number_format(2,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "remail_golds" => number_format($parent_buyer_account['remail_golds'] +2,2,".",""),
                                    "income_golds" => number_format($parent_buyer_account['income_golds'] +2,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }
                            //被邀请人账号信息
                            $invited_buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($task_order['buy_id']);
                            //被邀请人返利
                            $invited_arr = [
                                "buy_id" => $task_order['buy_id'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 3,
                                "balance" => number_format($invited_buy_account['remail_golds']+3,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $invited_buy_account['freezing_golds'],
                                "remark" => "被邀请人-买手-首次完成首单任务获取3花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $invited_buy_pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($invited_arr);
                            if(!$invited_buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],429,"增加被邀请人返利失败");
                            }

                            if(empty($invited_buy_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>3,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 3,
                                    "buy_id" => $task_order['buy_id'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $invited_arr = [
                                    "remail_golds" => number_format($invited_buy_account['remail_golds'] + 3,2,".",""),
                                    "income_golds" => number_format($invited_buy_account['income_golds'] + 3,2,".",""),
                                    "buy_id" => $task_order['buy_id'],
                                ];
                                $invited_buy_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($invited_arr);
                                if(!$invited_buy_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],430,"被邀请人返利失败");
                                }
                            }
                        }else
                        {
                            $inviter_arr = [
                                "buy_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 1,
                                "balance" => number_format($parent_buyer_account['remail_golds'] +1,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $parent_buyer_account['freezing_golds'],
                                "remark" => "被邀请人完成任务-邀请人买手获取1花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $buy_pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($inviter_arr);
                            if(!$buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],431,"邀请人买手增加返利记录失败");
                            }
                            if(empty($parent_buyer_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>1,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 1,
                                    "buy_id" => $buyer['pid'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $parent_arr = [
                                    "remail_golds" => number_format($parent_buyer_account['remail_golds'] +1,2,".",""),
                                    "income_golds" => number_format($parent_buyer_account['income_golds'] +1,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],432,"修改邀请人返利失败");
                                }
                            }
                        }
                    }else if($parentBuyUser['role'] ==2)//邀请人是商家
                    {
                        //邀请人是商家的情况  商家邀请买手  返利给商家  返利给买手
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($buyer['pid']);
                        //已经完成任务单数
                        $task_order_count = $this->RTaskOrderAccess->getBuyTaskOrderCount($buyer['id'],8);
                        if($task_order_count ==1)
                        {
                            $inviter_arr = [
                                "bus_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 2,
                                "balance" => number_format( $business_account['ava_pepper'] + 2,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $business_account['freezing_golds'],
                                "remark" => "买手首次邀请首次完成完成任务-邀请人商家获取2花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $business_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($inviter_arr);
                            if(!$business_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人商家增加返利记录失败");
                            }
                            if(empty($business_account))
                            {
                                $parent_arr = [
                                    "total_deposit" => 0,
                                    "ava_deposit" =>0,
                                    "freezing_deposit" => 0,
                                    "ava_pepper" =>2,
                                    "freezing_golds" => 0,
                                    "cash_deposit" => 0,
                                    "bus_id" => $buyer['pid'],
                                ];
                                $business_users = $this->WBusinessAcountAccess->addBusAccount($parent_arr);
                                if(!$business_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "total_deposit" => $business_account['total_deposit'],
                                    "freezing_deposit" => $business_account['freezing_deposit'],
                                    "ava_deposit" => $business_account['ava_deposit'],
                                    "ava_pepper" => number_format($business_account['ava_pepper'] +2,2,".",""),
                                    "freezing_golds" =>$business_account['freezing_golds'],
                                    "bus_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBusinessAcountAccess->modifyAcount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }
                            $invited_buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($task_order['buy_id']);
                            //被邀请人返利
                            $invited_arr = [
                                "buy_id" => $task_order['buy_id'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 3,
                                "balance" => number_format($invited_buy_account['remail_golds'] +3,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $invited_buy_account['freezing_golds'],
                                "remark" => "被邀请人-买手-首次邀请首次完成任务获取3花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $invited_buy_pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($invited_arr);
                            if(!$invited_buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],429,"增加被邀请人返利失败");
                            }

                            if(empty($invited_buy_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>3,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 3,
                                    "buy_id" => $task_order['buy_id'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $invited_arr = [
                                    "remail_golds" => number_format($invited_buy_account['remail_golds'] + 3,2,".",""),
                                    "income_golds" => number_format($invited_buy_account['income_golds'] + 3,2,".",""),
                                    "buy_id" => $task_order['buy_id'],
                                ];
                                $invited_buy_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($invited_arr);
                                if(!$invited_buy_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],430,"被邀请人返利失败");
                                }
                            }

                        }else//非首单
                        {
                            $inviter_arr = [
                                "bus_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 1,
                                "balance" => number_format( $business_account['ava_pepper'] + 1,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $business_account['freezing_golds'],
                                "remark" => "邀请人-商家-买手完成任务-非首次邀请非首单获取1花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $business_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($inviter_arr);
                            if(!$business_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人商家增加返利记录失败");
                            }
                            if(empty($business_account))
                            {
                                $parent_arr = [
                                    "total_deposit" => 0,
                                    "ava_deposit" =>0,
                                    "freezing_deposit" => 0,
                                    "ava_pepper" =>1,
                                    "freezing_golds" => 0,
                                    "cash_deposit" => 0,
                                    "bus_id" => $buyer['pid'],
                                ];
                                $business_users = $this->WBusinessAcountAccess->addBusAccount($parent_arr);
                                if(!$business_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "total_deposit" => $business_account['total_deposit'],
                                    "freezing_deposit" => $business_account['freezing_deposit'],
                                    "ava_deposit" => $business_account['ava_deposit'],
                                    "ava_pepper" => number_format($business_account['ava_pepper'] +1,2,".",""),
                                    "freezing_golds" =>$business_account['freezing_golds'],
                                    "bus_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBusinessAcountAccess->modifyAcount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }
                        }
                    }
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 6)//撤销商家返款
            {
                //判断子订单是否是待返款
                if($task_order['order_status'] != 7)
                {
                    return Tool::returnJson([],436,"非商家已返款状态不能撤销商家返款");
                }
                //进行撤销商家返款操作
                $arr = [
                    "order_status" => 6,
                    "task_order_id" => $data['task_order_id'],
                ];
                $res = $this->WTaskOrderAccess->revokeRefunds($arr);
                if(!$res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],437,"后台撤销商家返款失败");
                }
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $task_order['buy_id'],
                    "task_status" => 6,
                    "status_remark" => "已收货待返款",
                    "remark" => '买手已收货待商家返款',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],438,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }else if($data['type'] == 7)//撤销买手驳回
            {
                //判断子订单是否是待返款
                if($task_order['order_status'] != 9)
                {
                    return Tool::returnJson([],439,"非买手驳回状态不能撤销买手驳回");
                }
                $arr = [
                    "task_order_id" => $data['task_order_id'],
                    "order_status" => 7,
                ];
                $res = $this->WTaskOrderAccess->cancelReject($arr);
                if(!$res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],440,"后台撤销买手驳回");
                }
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $task_order['buy_id'],
                    "task_status" => 7,
                    "status_remark" => "已返款待确定",
                    "remark" => '商家已返款待买手确定',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $data['operator'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],441,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],442,"操作类型有误");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"操作异常".$e->getMessage());
        }
    }
}